<?php
namespace app\index\model;
use think\Db;
use think\Model;

class Register_model {
	public function doRegister() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		$res = $_POST;

		//查询用户名是否已存在
		$is_exist = Db::table('cn_user')
			->where('user_name', $res['username'])
			->field('user_id')
			->find();

		if ($is_exist) {
			return false;
		}
		// 写入cn_user
		$data = [
			'user_name' => $res['username'],
			'user_pass' => md5($res['pwd']),
			'user_email' => $res['email'],
			'user_working_place' => $res['address'],
			'user_mobile' => $res['mobile'],
			'user_office_phone' => $res['omobile'],
			'user_remarks' => $res['more'],
			'create_user_id' => $uid,
			'create_time' => $time,
			'update_user_id' => $uid,
			'update_time' => $time,
			'is_delete' => '0',
			'level' => $res['level'],
		];
		// var_dump($data);die;
		$list = db('_user')
			->insertGetId($data);

		// var_dump($list);die;
		$cres = [
			'user_id' => $list, // 缺少返回user_id
			'org_id' => $res['zuzhi'],
			'create_user_id' => $uid,
			'create_time' => $time,
			'update_user_id' => $uid,
			'update_time' => $time,
			'is_delete' => 0, //默认0
		];
		// var_dump($cres);die;
		$org = db('_user_organization_relation')
			->insert($cres);
		// var_dump($org);die;
		// $ocha = Db::table('cn_organization_character_relation')
		//         ->where('org_id',$res['zuzhi'])
		//         ->field('chara_id')
		//         ->find();

		// var_dump($ocha);die;
		// $ochar = $ocha['chara_id'];
		// var_dump($ochar);die;
		// //如果选择的是总分支  则组织默认的char_id是2 总监
		// if ($res['zuzhi'] == '0') {
		//         $ochar = '2';
		//     }
		// 写入cn_user_character_relation
		$cha = [
			'user_id' => $list,
			'chara_id' => 0,
			'create_user_id' => $uid,
			'create_time' => $time,
			'update_user_id' => $uid,
			'update_time' => $time,
			'is_delete' => 0, //默认0
		];
		$ucr = db('_user_character_relation')
			->insert($cha);
		// var_dump($ucr);die;
		$p = Db('_user_character_relation')
			->where('user_id', $list)
			->field('chara_id')
			->find();
		// var_dump($p);die;

		// // 根据查询到的组织对应的chara_id再去查询menu_id
		// $zm = Db::table('cn_menu_character_relation')
		//         ->where('chara_id',$ochar)
		//         ->field('menu_id')
		//         ->find();
		//         // var_dump($zm);die;
		// // 写入cn_menu_character_relation
		// $mcr = [
		//     'menu_id' => $zm['menu_id'],
		//     'chara_id' => $p['chara_id'],
		//     'create_user_id' => $list,
		//     'create_time' => $time,
		//     'update_user_id' => $list,
		//     'update_time' => $time,
		//     'is_delete' => 0 //默认0
		// ];
		// $cmcr = db('_menu_character_relation')
		//     ->insert($mcr);

		return $p;
	}

	public function BmRegister() {
		// var_dump($_POST);die;
		// 子部门
		$org_name = $_POST['son'];
		// 描述
		$org_desc = $_POST['desc'];
		// 地址
		$org_address = $_POST['address'];
		// 类型
		$org_type = $_POST['type'];
		// 上级分支名
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		if ($_POST['zuzhi'] == 0) {
			$rev = [
				'org_name' => $org_name,
				'org_desc' => $org_desc,
				'org_address' => $org_address,
				'org_type' => $org_type,
				'org_parent_id' => '0,',
				'path' => '0,',
				'create_time' => $time,
				'create_user_id' => $uid,
				'update_time' => $time,
				'is_delete' => '0',
			];

			// var_dump($rev);die;
			$info = db('_organization')
				->insertGetId($rev);
		} else {
			//拿父级path
			$fpath = Db::table('cn_organization')
				->where('org_id', $_POST['zuzhi'])
				->field('path')
				->find();
			$rev = [
				'org_name' => $org_name,
				'org_desc' => $org_desc,
				'org_address' => $org_address,
				'org_type' => $org_type,
				'org_parent_id' => $_POST['zuzhi'],
				'path' => $fpath['path'] . $_POST['zuzhi'] . ',',
				'create_time' => $time,
				'create_user_id' => $uid,
				'update_time' => $time,
				'is_delete' => '0',
			];
			$info = db('_organization')
				->insertGetId($rev);

		}
		//新创建的部门给角色
		// if ($info) {
		//     $cdata = [
		//         'org_id' => $info,
		//         'chara_id' => 3, //默认3  借阅
		//         'create_user_id' => $uid,
		//         'create_time' => $time,
		//         'update_user_id' => $uid,
		//         'update_time' => $time,
		//         'is_delete' => 0,
		//     ];
		//     $cha = Db::table('cn_organization_character_relation')
		//             ->insert($cdata);
		//     return $info;
		// }
		// return false;
		return $info;
	}

}
?>